<?php
/**
 * Created by PhpStorm.
 * User: 554665488
 * Date: 2018-6-24
 * Time: 16:02
 * @description:服务端
 * @Author: yfl
 * @QQ 554665488
 */
$server = new Swoole\Server('0.0.0.0', 9501, SWOOLE_PROCESS, SWOOLE_SOCK_TCP);

$server->set([
    'worker_num' => 4
]);
$server->on('workerStart', function ($serv, $worker_id) {
    if ($worker_id == 0) {
        $serv->after(10000, function () use ($serv) {
            foreach ($serv->connection_list() as $fd) {
                $serv->send($fd, $fd . '来参与互动');
            }
        });
    }
});

$server->on('connect', function ($serv, $fd) {
    echo "有新的客户端连接，连接标识为$fd" . PHP_EOL;
});

$server->on('receive', function ($serv, $fd, $from_id, $data) {
    $serv->send($fd, '向客户端发送数据');
});

$server->on('close', function ($serv, $fd) {
    echo "编号为{$fd}的客户端已经关闭." . PHP_EOL;
});


$server->start();
